﻿#include "breakdialog.h"
#include "ui_breakdialog.h"
#include "widget.h"
#include <QCloseEvent>
#include <QTimer>

BreakDialog::BreakDialog(QWidget *parent, int breakTime) :
    QDialog(parent),
    ui(new Ui::BreakDialog)
{
    ui->setupUi(this);
    this->setModal(true);

    timer=new QTimer(this);
    connect(timer, &QTimer::timeout, this, &BreakDialog::updateOneSec);
    timer->start(INTERVAL);
    curStep=0;
    breakSteps=breakTime;

    ui->progressBar->setMaximum(breakSteps);

    this->setWindowFlags(this->windowFlags()|Qt::FramelessWindowHint);
}

BreakDialog::~BreakDialog()
{
    delete ui;
    delete timer;
}

void BreakDialog::setPostoff(bool enable)
{
    ui->pushButton->setEnabled(enable);
}

void BreakDialog::closeEvent(QCloseEvent *event)
{
    this->reject();
    event->ignore();
}

void BreakDialog::updateOneSec()
{
    static int curSec=0;
    timer->stop();
    curSec=(curSec+1)%60;
    if(curSec==0)
        curStep=curStep+1;
    if(curStep<breakSteps)
    {
        ui->progressBar->setValue(curStep);
        ui->label->setText(QString::asprintf("%d/%d", curStep, breakSteps));
    }
    else
    {
        this->accept();
    }
    timer->start(INTERVAL);
}



void BreakDialog::on_pushButton_clicked()
{
    this->reject();
}


